package com.goldmantis.alitrip.controller.system;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.UserThreadLocal;
import com.goldmantis.alitrip.pojo.SysSubsystem;
import com.goldmantis.alitrip.service.SysSubsystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "子系统管理")
@RequestMapping("sys/subsystem")
@RequiresAuthentication

public class SysSubsystemController {

    @Autowired
    private SysSubsystemService sysSubsystemService;

    /**
     * 获取子系统
     * @param searchEntity
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "子系统列表",httpMethod = "GET")

    public ResponseEntity<ReturnMessage> list(
                                              @ApiParam("查询对象") SysSubsystem searchEntity,
                                              @ApiParam("当前页") @RequestParam(defaultValue = "1") Integer offset,
                                              @ApiParam("每页显示数量") @RequestParam(required = false) Integer limit) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysSubsystemService.pageList(searchEntity,offset,limit)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    @GetMapping(value = "{systemId}")
    @ApiOperation(value = "根据Id获取子系统",httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键",required = true) @PathVariable(required = true) Long systemId) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysSubsystemService.selectById(systemId)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 新增子系统
     * @param sysSubsystem
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增子系统",httpMethod = "POST")
    public ResponseEntity<ReturnMessage> post(@ApiParam("子系统对象") @RequestBody @Valid SysSubsystem sysSubsystem) {
        try {
            sysSubsystem.setCreatetime(new Date());
            sysSubsystem.setCreator(UserThreadLocal.get().getUserId().toString());
            return ResponseEntity.ok(ReturnMessage.success(sysSubsystemService.insert(sysSubsystem)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 编辑子系统
     * @param sysSubsystem
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑子系统",httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("子系统对象") @RequestBody SysSubsystem sysSubsystem) {
        try {
            sysSubsystem.setCreatetime(new Date());
            sysSubsystem.setCreator(UserThreadLocal.get().getUserId().toString());
            return ResponseEntity.ok(ReturnMessage.success(sysSubsystemService.update(sysSubsystem)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除子系统
     * @param subsystem_ids
     * @return
     */
    @DeleteMapping(value = "{subsystem_ids}")
    @ApiOperation(value = "批量删除子系统",httpMethod = "DELETE")
    @RequiresAuthentication
    public ResponseEntity<ReturnMessage> delete(@ApiParam("系统id 逗号隔开（1,2）") @PathVariable String subsystem_ids){
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(subsystem_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysSubsystemService.deleteByIds(ids,SysSubsystem.class,"systemid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


}
